package com.home.duy.code.DAO;

import java.util.List;
import java.util.Stack;

import org.hibernate.Criteria;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;

import com.home.Entity.Ban;
import com.home.Entity.CTQuyenSuDung;
import com.home.System.HibernateUtil;
import com.home.duy.code.Abstract.AbstractDAO;

public class BanDAO extends AbstractDAO{
	private Session session;
	private Transaction trans;
	public BanDAO()
	{
		super();
	}
	
	@Override
	public boolean saveOrUpdate(Object obj) {
		return super.saveOrUpdate(obj);
	}

	@Override
	public boolean delete(Object obj) {
		return super.delete(obj);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Object findByPk(Class clas, long pk) {
		return super.findByPk(clas, pk);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List findAll(Class clas) {
		return super.findAll(clas);
	}
	
	@SuppressWarnings("unchecked")
	public List<Ban> findByKVB(long kvbPk) {
		startOperation();
		List<Ban> list = null;
		try {
			list = session
			.createSQLQuery(
					"SELECT * FROM BAN WHERE BAN_KVB = "
							+ kvbPk)
			.addEntity(Ban.class).list();
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			session.close();
		}
		
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Ban> findByPaging(int maxResult,int from) {
		startOperation();
		List<Ban> list = null;
		try {
			list = session.createCriteria(Ban.class).setMaxResults(maxResult).setFirstResult(from).list();
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return list;
	}
	
	public Ban findByFirstRecord() {
		startOperation();
		Ban ban = null;
		try {
			ban = (Ban)session.createSQLQuery("SELECT TOP 1* FROM BAN").addEntity(Ban.class).uniqueResult();
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return ban;
	}
	
	public int countSizeData()
	{
		startOperation();
		int count = 0;
		try {
			Criteria criteria = session.createCriteria(Ban.class);
			criteria.setProjection(Projections.rowCount());
			count = Integer.parseInt(criteria.uniqueResult().toString());
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		return count;
	}
	
	// ================= TRUC SECTION ===========================
	
	//phuong thuc tim danh sach ban trong mot khu vuc ban
	@SuppressWarnings("unchecked")
	public List<Ban> TimBanTheoKhuVuc(long kv)
	{
		startOperation();
		List<Ban> LBan = new Stack<Ban>();
		try {
			List<Ban> L = (List<Ban>)findAll(Ban.class);
			for(Ban B : L)
			{
				if(B.getKhuVucBan().getMaSo() == kv)
					LBan.add(B);
			}
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			session.close();
		}
		
		return LBan;
	}
	//phuong thuc tim danh sach ban ghep
	@SuppressWarnings("unchecked")
	public List<Ban> TimDSBanGhep(long maBanGhep)
	{
		startOperation();
		List<Ban> list = null;
		try {
			list = session.createSQLQuery(
					"SELECT * FROM BAN WHERE BAN_BANGHEP = "
							+ maBanGhep)
			.addEntity(Ban.class).list();
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			session.close();
		}
		return list;
	}
	//phuong thuc cap nhat trang thai ban (goi mon)
	public void CapNhatTrangThai(Ban b)
	{
		try {
			int c = new TTGoiMonDAO().DemSoMonGoi(b.getMaSo());
			if(c>0)
				b.setTrangThai(true);
			else
				b.setTrangThai(false);
			this.saveOrUpdate(b);	
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	//phuong thuc tim danh sach ban duoc su dung khong phan biet ban ghep
	@SuppressWarnings("unchecked")
	public List<Ban> TimDSBanDuocSuDung()
	{
		startOperation();
		List<Ban> list = null;
		try {
			list = session.createSQLQuery(
					"SELECT * FROM BAN WHERE BAN_SUDUNG = 'true'")
			.addEntity(Ban.class).list();
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			session.close();
		}
		return list;
	}
	
	public List<Ban> TimDSBanSuDung()
	{
		List<Ban> L = TimDSBanDuocSuDung();
		
		int count = L.size();
		for(int i = 0;i<count;i++)
		{
			if(L.get(i).getBanGhep() != 0)
			{
				for(int k = i+1;k<count;k++)
				{
					if(L.get(k).getBanGhep() == L.get(i).getBanGhep())
					{
						L.remove(k);
						count--;
						k--;
					}
				}
			}
		}
		
		return L;
	}
	// ================= END TRUC SECTION ===================
	
	private void startOperation()
	{
		session = HibernateUtil.getSessionFactory().openSession();
		trans = session.beginTransaction();
	}
}
